package com.aaa.controller;

import com.aaa.entity.ResponseDto;
import com.aaa.entity.Staff;
import com.aaa.service.StaffService;
import com.aaa.service.impl.StaffServiceImpl;
import com.aaa.util.DateUtils;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/GetUserByNearYearServlet")
public class GetUsertByNearYearServlet extends HttpServlet {
    StaffService staffService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int[] months= DateUtils.getMonthByNearYear();
        for (int i = 0; i <months.length ; i++) {
            System.out.println(months[i]);
        }
        resp.setContentType("application/json");
        Integer pageNumber = IntegerUtils.ToInteger(req.getParameter("pageNumber"));
        Integer pageSize = IntegerUtils.ToInteger(req.getParameter("pageSize"));
        String searchStaffId = req.getParameter("searchStaffId");
        String searchName = req.getParameter("searchName");

        List<Staff> list = staffService.findAllStaff(pageNumber,pageSize,searchStaffId,searchName);
        int count = staffService.getAllStaffInfoCount(searchStaffId,searchName);
        System.out.println(count);
        ResponseDto responseDto = new ResponseDto();
        if (list != null){
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setCount(count);
            responseDto.setData(list);
            responseDto.setMessage("操作成功");
        }else {
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("操作失败");
        }
        System.out.println(list);
        resp.getWriter().write(new Gson().toJson(responseDto));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        staffService = new StaffServiceImpl();
    }
}
